package com.qys.livesMall.app.user.scheduledTask;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.model.entity.H2AppUserRechargeWithdrawals;
import com.qys.livesMall.app.user.model.entity.UserStatisticsYesterdayInfo;
import com.qys.livesMall.app.user.service.IH2AppUserRechargeWithdrawalsService;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.app.user.service.UserStatisticsYesterdayInfoService;
import com.qys.livesMall.common.utils.DateUtils;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户统计
 */
@Component("UserStatisticsTask")
@Slf4j
@AllArgsConstructor
public class UserStatisticsScheduledTask {

    @Resource
    private IH2AppUserRechargeWithdrawalsService rechargeWithdrawalsService;
    @Resource
    private IH2AppUserService ih2AppUserService;
    @Resource
    private UserStatisticsYesterdayInfoService userStatisticsYesterdayInfoService;
    /**
     * 统计用户充值、提现、注册
     */
    public void UserStatisticsTask() {
        //查询昨天的充值提现数据
        List<H2AppUserRechargeWithdrawals> withdrawalsList = rechargeWithdrawalsService.getYesterday();
        //昨天注册的用户
        List<H2AppUser> users=ih2AppUserService.getYesterday();
        List<H2AppUser> usersAll=ih2AppUserService.userAll();
        for (int i = 0; i < 2; i++) {
            int userSysType=i;
            List<String> userIds=usersAll.stream().filter(a->a.getUserSysType().intValue() == userSysType ).map(H2AppUser::getId).toList();
            //昨天统计的数据
            UserStatisticsYesterdayInfo yesterdayInfo=userStatisticsYesterdayInfoService.getYesterday(0,userSysType);
            UserStatisticsYesterdayInfo dayInfo=new UserStatisticsYesterdayInfo();
            dayInfo.setRegister(users.size());
            dayInfo.setType(0);
            dayInfo.setUserSysType(userSysType);
            if (CollectionUtils.isNotEmpty(withdrawalsList)) {
                //充值金额
                BigDecimal re = withdrawalsList.stream().filter(a -> a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getType()) &&  userIds.contains(a.getUserId())).map(a -> a.getRealAmount()).reduce(
                        BigDecimal.ZERO, BigDecimal::add
                );
                //提现金额
                BigDecimal wl = withdrawalsList.stream().filter(a -> a.getType().equals(AccountBusinessTypeEnum.WITHDRAWAL.getType()) &&  userIds.contains(a.getUserId())).map(a -> a.getRealAmount()).reduce(
                        BigDecimal.ZERO, BigDecimal::add
                );
                //充值差
                BigDecimal differ = re.subtract(wl);

                List<String> list = withdrawalsList.stream().filter(a -> a.getType().equals(AccountBusinessTypeEnum.RECHARGE.getType()) &&  userIds.contains(a.getUserId())).map(H2AppUserRechargeWithdrawals::getUserId).distinct().collect(Collectors.toList());
                Integer one =0;
                Integer many =0;
                if (CollectionUtils.isNotEmpty(list)){
                    //首充
                    one = rechargeWithdrawalsService.WithdrawalstByUserIds(list, 1);
                    //复冲
                    many = rechargeWithdrawalsService.WithdrawalstByUserIds(list, 2);
                }


                //提现人数
                Long wlnum = withdrawalsList.stream().filter(a -> a.getType().equals(AccountBusinessTypeEnum.WITHDRAWAL.getType()) &&  userIds.contains(a.getUserId()) ).map(H2AppUserRechargeWithdrawals::getUserId).distinct().count();

                dayInfo.setRecharge(re);
                dayInfo.setWithdrawal(wl);
                dayInfo.setDiffer(differ);
                dayInfo.setFirstRecharge(one);
                dayInfo.setManyRecharge(many);
                dayInfo.setWithdrawalPeople(wlnum.intValue());

                if (null !=yesterdayInfo){
                    dayInfo.setTotalRecharge(re.add(yesterdayInfo.getTotalRecharge()));
                    dayInfo.setTotalWithdrawal(wl.add(yesterdayInfo.getTotalWithdrawal()));
                    dayInfo.setTotalDiffer(differ.add(yesterdayInfo.getTotalDiffer()));
                    dayInfo.setTotalFirstRecharge(one + yesterdayInfo.getTotalFirstRecharge());
                    dayInfo.setTotalManyRecharge(yesterdayInfo.getTotalManyRecharge() + many);
                    dayInfo.setTotalWithdrawalPeople(wlnum.intValue() + yesterdayInfo.getTotalWithdrawalPeople());
                    dayInfo.setTotalRegister(userIds.size());
                }else {
                    dayInfo.setTotalRecharge(re);
                    dayInfo.setTotalWithdrawal(wl);
                    dayInfo.setTotalDiffer(differ);
                    dayInfo.setTotalFirstRecharge(one);
                    dayInfo.setTotalManyRecharge(many);
                    dayInfo.setTotalWithdrawalPeople(wlnum.intValue());
                    dayInfo.setTotalRegister(userIds.size());
                }
            }
            userStatisticsYesterdayInfoService.save(dayInfo);
        }
    }
}
